package cn.shengongying.admin.controller;

import cn.shengongying.utils.UploadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.util.Map;

/**
 * author: RXK
 * date: 2022/2/9 星期三
 * desc:
 */
abstract class BaseController {
    protected Logger logger = LoggerFactory.getLogger(getClass());


    protected String multiFile(HttpServletRequest request) throws Exception {
        if (request instanceof MultipartHttpServletRequest) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
            if (fileMap.size() > 0) {
                for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
                    MultipartFile multipartFile = entity.getValue();
                    BufferedImage bi = ImageIO.read(multipartFile.getInputStream());
                    if (!multipartFile.isEmpty() && bi != null) {
                        return UploadUtils.upload(multipartFile.getInputStream());
                    } else {
                        throw new Exception("上传的文件不安全，保存失败！");
                    }
                }
            }
        }
        return null;
    }
}
